import Player from "../player/Player";
import MoveComp from "../funcComp/MoveComp";
import AiPlayer from "../player/AiPlayer";
import ControlledPlayer from "../player/ControlledPlayer";
import BadgerPlayer from "../player/BadgerPlayer";
import { Global } from "../info/Global";
import DestructibleFacility from "../facility/DestructibleFacility";
import { PlayerID } from "../info/GameEnum";
import { partitionStartPoints } from "../info/Predefine";
import Utils from "../../Utils";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;

/**
 * 管理所有玩家
 */
@ccclass
export default class PlayerManager extends cc.Component {

    public static instance: PlayerManager;

    @property(cc.Prefab)
    playerPerfab: cc.Prefab = null;

    playerParent: cc.Node = null;

    controllerPlayer: ControlledPlayer = null;

    badgerPlayer: BadgerPlayer = null;

    @property([cc.AnimationClip])
    playerAnimation: cc.AnimationClip[] = [];

    public get playerComps(): Player[] {
        let comps = []
        this.playerParent.children.forEach(node => {
            node.active && comps.push(node.getComponent(Player));
        })
        return comps;
    };

    /**
     * 所有用户下的设施
     */
    get allFacility(): DestructibleFacility[] {
        let ret = [];
        for (let i = 0; i < this.playerComps.length; i++) {
            ret.push(this.playerComps[i].allFacility);
        }
        return ret.flat();
    }

    protected onLoad(): void {
        PlayerManager.instance = this;

        //添加玩家父节点
        this.playerParent = new cc.Node("playerParent");
        this.playerParent.parent = this.node;

        //添加玩家节点
        let gameinfo = Global.currentGameData;
        let player = cc.instantiate(this.playerPerfab);
        let playerId = Global.getPlayerId(gameinfo.self.roleId);
        this.controllerPlayer = player.addComponent(ControlledPlayer);
        this.controllerPlayer.init(gameinfo.self.roleId, gameinfo.self.props);
        player.addComponent(MoveComp).init(this.controllerPlayer, true, Global.roleMoveSpeed);

        this.playerParent.addChild(player);
        let anim = player.getComponentInChildren(cc.Animation);
        let ass = this.playerAnimation[this.controllerPlayer.roleId];
        anim.addClip(ass);
        anim.play(ass.name);

        //其他AI玩家节点
        for (let i = 0; i < gameinfo.enemy.length; i++) {
            if (gameinfo.enemy[i].hide || i == playerId) continue;
            let roleId = gameinfo.enemy[i].roleId
            let playerNode = cc.instantiate(this.playerPerfab);
            let anim = playerNode.getComponentInChildren(cc.Animation);
            let playerComp = playerNode.addComponent(AiPlayer);
            playerComp.init(roleId, gameinfo.enemy[i].props);
            playerNode.addComponent(MoveComp).init(playerNode.getComponent(Player), true, Global.roleMoveSpeed);
            this.playerParent.addChild(playerNode);

            let ass = this.playerAnimation[roleId];
            anim.addClip(ass);
            anim.play(ass.name);
        }

        //创建平头哥节点
        let badgerPlayerNode = new cc.Node("badgerPlayer");
        this.badgerPlayer = badgerPlayerNode.addComponent(BadgerPlayer);
        this.badgerPlayer.init();
    }

    protected start(): void {
        this.playerParent.setSiblingIndex(2);

        let poins = partitionStartPoints.concat();
        // 初始化位置
        this.playerParent.children.forEach(item => {
            let index = Utils.getRandom(0, poins.length - 1);
            let block = poins[index];
            poins.splice(index, 1);
            block.locationX += Utils.getRandom(0, 19);
            block.locationY += Utils.getRandom(0, 39);
            item.position = GameManager.instance.getGamePosition(block).add(cc.v3(Utils.getRandom(-80, 80), Utils.getRandom(-80, 80)))
        })
    }

    /**
     * 玩家是否获胜
     * @returns 
     */
    isWiner(): boolean {
        let livePlayer = this.playerParent.children.filter(player => player.active)
        return livePlayer.length == 1 && this.controllerPlayer.node === livePlayer[0];
    }

    protected onDestroy(): void {
        PlayerManager.instance = null;
    }

    public static getRoleIds(playerid: PlayerID): number[] {
        switch (playerid) {
            case PlayerID.player1: //黄
                return [0, 1, 2];
            case PlayerID.player2: //红
                return [3, 4];
            case PlayerID.player3: //绿
                return [5, 6];
            case PlayerID.player4: //橙
                return [7, 8];
            case PlayerID.player5: //紫
                return [9, 10];
            case PlayerID.player6: //蓝
                return [11, 12];
            default:
                return [];
        }
    }

}
